package Controller;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import Entity.Department;
import Model.DepartmentManageModel;
import View.DepartmentManageView;


public class DepartmentManageController {
	private boolean update = false;
	private DepartmentManageView view ;
	private DepartmentManageModel model;
	private JFrame frame;

	public DepartmentManageController(){
		super();
			frame = new JFrame();
			frame.setSize(800, 600);
			view = new DepartmentManageView();
			model = new DepartmentManageModel();	
			view.setModel(model);
			doAction();
			frame.add(view);
			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			Dimension screensize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
			frame.setBounds(screensize.width/2-400,screensize.height/2 - 300,800,600);
			frame.setVisible(true);
	}

	private void doAction() {
		// TODO Auto-generated method stub
		
		doAdd();
		doWrite();
		doEdit();
		doDelete();
		loadTable();
		doExit();
		doSelectTable();
	}
	
	private void loadTable() {
		// TODO Auto-generated method stub
		try {
			model.loadDepartmentList();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doSelectTable() {
		// TODO Auto-generated method stub
		view.doSelectTable(new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				doSelect();
			}

		});

		view.doSelectTable(new MouseAdapter() {

			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				doSelect();
			}

		});
	}
	
	private void doSelect() {
		int row = view.getSelectedItem();
		Department department = model.getDepartmentList().get(row);
		view.display(department);
		view.setEnableControl(false);
	} 
	
	private void doAdd() {
		// TODO Auto-generated method stub
		view.doAdd(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				Department department = view.getData();
				int row = view.getSelectedItem();
				if (department.getName().trim().equals("")) {
					JOptionPane.showMessageDialog(frame,
							"Ban chua nhap ten cho Department");
					return;
				}
				if(update){
					try {
						model.editDepartment(row, department);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				else {
					try {
						model.addDepartment(department);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				view.setEnableControl(false);
		}
	});	
	view.setEnableControl(false);
}
	
	private void doWrite() {
		// TODO Auto-generated method stub
		view.doWrite(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				view.display(new Department("",0));
				view.setEnableControl(true);
			}

		});

	}
	
	private void doEdit() {
		// TODO Auto-generated method stub
		view.doEdit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				update = true;
				view.setEnableControl(true);
			}

		});
	}

	private void doDelete() {
		// TODO Auto-generated method stub
		view.doDelete(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int row = view.getSelectedItem();
				Department department = view.getData();
				try {
					model.deleteDepartment(row, department);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}

		});
	}

	private void doExit() {
		// TODO Auto-generated method stub
		view.doExit(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				frame.dispose();
			}

		});
	}

	
	public static void main(String[] arg) throws IOException, ClassNotFoundException, SQLException  {
		new DepartmentManageController();
	}
}
